import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;


import java.awt.Font;

import java.awt.Polygon;
import java.io.PrintStream;

import java.util.Locale;
import java.util.Random; 



import pluginUtils.Circle;
import pluginUtils.Complexe; 
import pluginUtils.MobiusTransform;
import pluginUtils.CircleInversion;


public class Steiner2 extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=10000,Y=10000; // image size
	private double a=0.6;
	private double b=0.1;
	
	private double xShift=0; 
	private double zShift=0; 
	private double scale=1; 
	
	
//	 Inverter
	public Circle invCircle=new Circle(Complexe.createComplexeFromRI(1,1),1); 
	public CircleInversion cinv=new CircleInversion(invCircle);
	
	private PrintStream output; 
	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	private String[] palette={"Yellow","Red","Blue","Green","White","NavyBlue","Turquoise","Orange"};
	private Color[] palette1={Color.YELLOW,Color.RED,Color.BLUE,Color.GREEN,Color.MAGENTA,Color.ORANGE};
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	
	
	
	private void drawColoredCircle(Circle c,int i){
		double xCenter=c.getCenter().getReal(); 
		double yCenter=c.getCenter().getIm(); 
		//if(c.getRadius()<0.001) return ; 
		
		int intX=(int)(scale*((xCenter-xShift)*acoef)+ccoef);
		int intY=(int)(scale*((yCenter-zShift)*vcoef)+wcoef);
		int intRx=(int)(c.getRadius()*acoef*scale);
		int intRy=(int)(c.getRadius()*vcoef*scale);
		
		//primeIP.setColor(palette1[i]); 
		primeIP.drawOval(intX-intRx,intY-intRy,intRx*2,intRy*2);
		
		String s="sphere{\n<"+xCenter+","+yCenter+",0>,";
		s+=c.getRadius(); 
		s+=" texture{Texture"+i+"}finish{Finish"+i+"}}"; 
		
		output.println(s);
		
	}
	

	private void drawPoint(Complexe c){
		double xCenter=c.getReal(); 
		double yCenter=c.getIm(); 
		int intX=(int)(xCenter*acoef+ccoef);
		int intY=(int)(yCenter*vcoef+wcoef);
		primeIP.fillOval(intX-10,intY-10,20,20);
	}
	
	
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	private void steiner(Circle A, int nCircle, int level){
		if(level<0) return; 
		double externalRadius=A.getRadius(); 
		double internalRadius=Math.exp(Math.log(externalRadius)-2*Math.log(Math.tan(Math.PI/4+(Math.PI/(2*nCircle)))));
		Circle internal=new Circle(A.getCenter(),internalRadius); 
		
		//drawColoredCircle(A,0); 
		double distance=(externalRadius+internalRadius)/2; 
		double rayon=(externalRadius-internalRadius)/2;
		double xs=A.getCenter().getReal(); 
		double ys=A.getCenter().getIm(); 
		//System.out.println(level+" "+rayon);
		
		drawColoredCircle(A,2); 
		
		for(int i=0;i<nCircle;i++){
			double x=distance*Math.cos(2*i*Math.PI/nCircle); 
			double y=distance*Math.sin(2*i*Math.PI/nCircle); 
			Circle couronne=new Circle(Complexe.createComplexeFromRI(x+xs,y+ys),rayon);
			//if(level==0)
			//	drawColoredCircle(cinv.inverte(couronne),level);
			//else
				steiner(couronne,nCircle,level-1); 
			}
			//if(level==0)
			//	drawColoredCircle(cinv.inverte(internal),3);
			//else
				steiner(internal,nCircle,level-1); 
			return; 
			
		}
		
	
	
		
		
		
	
	
	
	
	public void run(String arg) {
		primeIP = new ColorProcessor(X,Y);
		
		
		double limit=1.02; 
	
		setBounds(-limit,limit,-limit,limit); 
		setCoefs(); 
		
		
		
		
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(4); 
		
		
		
		
		
		setProcessor("Verifying", primeIP);
		
		//drawLine(x0,0,x1,0);
		//drawLine(0,y0,0,y1);
		
		try{
			output=new PrintStream("../pearls/scene/doyle/steiner.txt"); 
			Circle externe=new Circle(Complexe.ZERO,1); 
			//Circle interne=new Circle(Complexe.ZERO,beta); 
			drawColoredCircle(externe, 1); 
			xShift=cinv.inverte(externe).getCenter().getReal();
			zShift=cinv.inverte(externe).getCenter().getIm();
			scale=externe.getRadius()/cinv.inverte(externe).getRadius();
			System.out.println(scale+" ***"); 
			//scale=1/scale;
			
			steiner(externe,6,5); 
		
		}
		catch (Exception e) {
			System.out.println("plante"); 
		}
		
		
		show();
		System.out.println("done "); 
	}	
}
	